package com.dpiinc.TechPage.user;


import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.dpiinc.TechPage.AIWriteApplication;
import com.dpiinc.TechPage.BaseActivity;
import com.dpiinc.TechPage.LoginActivity;
import com.dpiinc.TechPage.R;
import com.dpiinc.TechPage.activity.BottomBarActivity;
import com.dpiinc.TechPage.room.RoomAiWriterDatabase;
import com.dpiinc.TechPage.room.bean.UserEntity;
import com.dpiinc.TechPage.widget.Html5Webview;

import java.util.List;
import java.util.Locale;

import androidx.annotation.Nullable;


public class CommonWebviewActivity extends BaseActivity {

    private Html5Webview h5_webview;
    public static final String URL_KEY = "url_key";
    public static final String TITLE_KEY = "title_key";
    private String url;
    private String title;

    /**
     * 跳转到Webview
     *
     * @param context Context
     * @param url     url地址
     */
    public static void webviewEntrance(Context context, String url, String title) {
        Intent intent = new Intent(context, CommonWebviewActivity.class);
        intent.putExtra(URL_KEY, url);
        intent.putExtra(TITLE_KEY, title);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_common_webview);
        super.onCreate(savedInstanceState);
        initViews();
        //获取配置语言，未配置为默认语言
        String app_language = getSharedPreferences("app_language",Context.MODE_PRIVATE).getString("lauType",Locale.getDefault().getLanguage());
        Resources resources = getResources();
        Configuration config = resources.getConfiguration();
        config.locale = new Locale(app_language,"");
        resources.updateConfiguration(config,resources.getDisplayMetrics());
        AIWriteApplication.Companion.instance().updateLanguage();
    }


    private void init() {
        Intent intent = getIntent();
        url = intent.getStringExtra(URL_KEY);
        title = intent.getStringExtra(TITLE_KEY);
    }

    Button bt_h5_btn;

    public void initViews() {
        init();
        h5_webview = (Html5Webview) findViewById(R.id.h5_webview);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        bt_h5_btn = (Button) findViewById(R.id.h5_btn_tv);

//        if (url.equals(Constant.APP_PROTOCOL)) {
//            tv_title.setText("用户协议");
//        }else if(url.equals(Constant.APP_PROTOCOL_EN)){
//            tv_title.setText("Agreement");
//        } else {
//            tv_title.setText("");
//        }
        tv_title.setText(title);
        if (TextUtils.isEmpty(url)) {
//            toast("url不能为空");
            return;
        }

        boolean allow_privacy = getSharedPreferences("app_language", Context.MODE_PRIVATE).getBoolean("allow_privacy", false);
        if(title.equals("用户协议和隐私政策") && !allow_privacy){
            //隐私政策的界面
            //同时没有点击确认过的...显示按钮  并且点击跳转主界面
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    bt_h5_btn.setVisibility(View.VISIBLE);
                }
            },1000);
            bt_h5_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    getSharedPreferences("app_language",Context.MODE_PRIVATE).edit().putBoolean("allow_privacy",true).commit();
                    jumpToPage();
                }
            });
        }else{
            bt_h5_btn.setVisibility(View.GONE);
        }
//        h5_webview.getSettings().setJavaScriptEnabled(true);//使页面js生效
        h5_webview.loadUrl(url);

    }

    private boolean checkUserValid() {
        RoomAiWriterDatabase db = RoomAiWriterDatabase.getInstance(this);
        List<UserEntity> list = db.userDao().getAll();
        return (list != null && list.size() > 0);
    }

    private void jumpToPage() {
        if (!checkUserValid()) {
            startActivity(new Intent(this, LoginActivity.class));
        }else{
            startActivity(new Intent(this, BottomBarActivity.class));
        }
        finish();
    }

    @Override
    protected void onPause() {
        h5_webview.onPause();
        h5_webview.pauseTimers();
        super.onPause();
    }

    @Override
    protected void onResume() {
        h5_webview.onResume();
        h5_webview.resumeTimers();
        super.onResume();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
        if ((keyCode == KeyEvent.KEYCODE_BACK) && h5_webview.canGoBack()) {
            h5_webview.goBack();
            return true;
        }
        // If it wasn't the Back key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        return super.onKeyDown(keyCode, event);
    }
}